package com.jianggy.proxy;

import java.sql.SQLException;

/**
 * @author jianggy
 * @time 2018年9月23日 下午11:02:59
 * @description
 */
public class UserDaoProxy implements UserDao {
	private UserDaoImpl userDao = new UserDaoImpl();

	@Override
	public User getUser(long id) {
		User user = null;
		try {
			UserDaoImpl.connection.setAutoCommit(false);
			System.out.println("开始手动事务。。。");
			user = userDao.getUser(id);
			UserDaoImpl.connection.commit();
			System.out.println("提交事务。。。");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;

	}

	@Override
	public void insertUser(User user) {
		try {
			UserDaoImpl.connection.setAutoCommit(false);
			System.out.println("开始手动事务。。。");
			userDao.insertUser(user);
			UserDaoImpl.connection.commit();
			System.out.println("提交事务。。。");
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void deleteUser(int id) {
		try {
			UserDaoImpl.connection.setAutoCommit(false);
			System.out.println("开始手动事务。。。");
			userDao.deleteUser(id);
			UserDaoImpl.connection.commit();
			System.out.println("提交事务。。。");
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void updateUser(User user) {
		try {
			UserDaoImpl.connection.setAutoCommit(false);
			System.out.println("开始手动事务。。。");
			userDao.updateUser(user);
			UserDaoImpl.connection.commit();
			System.out.println("提交事务。。。");
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
